package cn.jhz.learn.blog.service.security.permission.impl;

import cn.jhz.learn.blog.dao.account.AccountRoleMapper;
import cn.jhz.learn.blog.dao.account.AccountUserRoleMapper;
import cn.jhz.learn.blog.po.account.AccountRole;
import cn.jhz.learn.blog.service.security.permission.RoleService;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service("roleService")
public class RoleServiceImpl implements RoleService {

    private final AccountRoleMapper roleMapper;
    private final AccountUserRoleMapper userRoleMapper;

    @Autowired
    public RoleServiceImpl(AccountRoleMapper roleMapper, AccountUserRoleMapper userRoleMapper) {
        this.roleMapper = roleMapper;
        this.userRoleMapper = userRoleMapper;
    }

    @Override
    public Set<AccountRole> getRolesByUserId(int userId) {
        List<Integer> roleIds = userRoleMapper.selectRolesIdByUserId(userId);
        Set<AccountRole> roles = Sets.newConcurrentHashSet();
        if(roleIds.size() > 0)
            roles.addAll(roleMapper.selectByPrimaryKeys(roleIds));

        return roles;
    }
}
